#include("../../layout/header.html")
#@layout()
#define content()
<body>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格工具栏 -->
            <form class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">关键字:</label>
                        <div class="layui-input-inline">
                            <input name="searchValue" class="layui-input" placeholder="缓存键支持*号查询"/>
                        </div>
                    </div>
                    <div class="layui-inline">&emsp;
                        <button class="layui-btn icon-btn" lay-filter="roleTbSearch" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>

                        <!--<button class="layui-btn layui-btn-warm icon-btn" type="reset">
                            <i class="layui-icon layui-icon-refresh"></i>清空筛选
                        </button>-->
                    </div>
                </div>
            </form>
            <!-- 数据表格 -->
            <table id="roleTable" lay-filter="roleTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="roleTbBar">
<!--    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>-->
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="roleEditDialog">
    <form id="roleEditForm" lay-filter="roleEditForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">数据类型:</label>
            <div class="layui-input-block">
                <select id="type2" name="type" lay-filter="type">
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">缓存键KEY:</label>
            <div class="layui-input-block">
                <input name="key" placeholder="请输入键" class="layui-input" lay-verType="tips" lay-verify="required"
                       required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">失效时间（秒）:</label>
            <div class="layui-input-block">
                <input name="expire" placeholder="只能输入非0整数，负数代表永久" class="layui-input" lay-verType="tips"
                       lay-verify="required" required/>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">缓存值VALUE:</label>
            <div class="layui-input-block">
                <textarea name="val" placeholder="" class="layui-textarea"></textarea>
            </div>
        </div>

        <!--<div class="layui-form-item text-right ai-form-footer">
            <button class="layui-btn" lay-filter="roleEditSubmit" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>-->
    </form>
</script>

<script type="text/html" id="redisTypeForm1">
    <form id="form1" lay-filter="roleEditForm" class="layui-form model-form">
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">缓存值VALUE:</label>
            <div class="layui-input-block">
                <textarea name="valStr" placeholder="请输入备注" class="layui-textarea"></textarea>
            </div>
        </div>
    </form>
</script>
<!-- js部分 -->
<script type="text/javascript" src="/static/libs/layui/layui.js"></script>
<script type="text/javascript" src="/static/js/common.js?v=318"></script>
<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin', 'zTree', 'formX'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var formX = layui.formX;
        var admin = layui.admin;

        var redisType = [];
        admin.req('/sys/sysDictData/get', {type: 'redisType'}, function (d) {
            if (0 === d.code) {
                redisType = d.data;
                formX.renderSelect({
                    elem: '#type',
                    data: redisType,
                    name: 'name',
                    value: 'name',
                    hint: '请选择类型'
                });
            }
        })

        /* 渲染表格 */
        var insTb = table.render({
            elem: '#roleTable',
            url: '/sys/sysRedis/list',
            page: true,
            toolbar: ['<p>',
                '<button lay-event="add" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>&nbsp;',
                '</p>'].join(''),
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox'},
                {type: 'numbers'},
                {field: 'type', title: '类型', sort: true},
                {field: 'key', title: '缓存键', sort: true,templet:function (d) {
                        return '<span class="layui-text"><a lay-event="info">'+d.key+'</a></span>';
                    }},
                {field: 'expire', title: '失效时间'},
                {field: 'elmNum', title: '元素总数'},

                {title: '操作', toolbar: '#roleTbBar', align: 'center', width: 200, minWidth: 200}
            ]]
        });

        /* 表格搜索 */
        form.on('submit(roleTbSearch)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}});
            return false;
        });

        /* 表格工具条点击事件 */
        table.on('tool(roleTable)', function (obj) {
            if (obj.event === 'edit') { // 修改
                showEditModel(obj.data);
            } else if (obj.event === 'del') { // 删除
                doDel(obj);
            } else if(obj.event === 'info'){
                showEditModel(obj.data);
            }
        });

        /* 表格头工具栏点击事件 */
        table.on('toolbar(roleTable)', function (obj) {
            if (obj.event === 'add') { // 添加
                showEditModel();
            } else if (obj.event === 'del') { // 删除
                var checkRows = table.checkStatus('roleTable');
                if (checkRows.data.length === 0) {
                    layer.msg('请选择要删除的数据', {icon: 2});
                    return;
                }
                var ids = checkRows.data.map(function (d) {
                    return d.id;
                });
                doDel({ids: ids});
            }
        });

        /* 显示表单弹窗 */
        function showEditModel(mData) {
            console.log(mData)
            admin.popupRight({
                type: 1,
                title: (mData ? '修改' : '添加') + '缓存',
                area: '450px',
                content: $('#roleEditDialog').html(),
                success: function (layero, dIndex) {
                    // 渲染搜索栏中的性别select
                    formX.renderSelect({
                        elem: '#type2',
                        data: redisType,
                        name: 'name',
                        value: 'name',
                        initValue: mData ? mData.type : "",
                        hint: '请选择数据类型'
                    });

                    // renderVal(mData.type);

                    // 回显表单数据
                    form.val('roleEditForm', mData);
                    renderVal2(mData);

                    /*因为redis键值类型的特殊 根据类型加载不同的表单*/
                    let typeValue = $('#type2').val();
                    console.log(typeValue);
                    renderVal(typeValue);


                    // 表单提交事件
                    /*form.on('submit(roleEditSubmit)', function (data) {
                        var loadIndex = layer.load(2);
                        admin.post('/sys/sysRedis/add', data.field, function (res) {
                            layer.close(loadIndex);
                            if (0 === res.code) {
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1});
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        });
                        return false;
                    });*/
                }
            });
        }

        function renderVal2(data) {
            let val = '';
            if(data.valStr){
                val = data.valStr
            }else if(data.valList){
                val = data.valList
            }else if(data.valMap){
                val = data.valMap
            }else if(data.valSet){
                val = data.valSet
            }
            console.log(val)
            $('textarea[name=val]').val(val);
        }

        function renderVal(data) {
            let val = '',type = data.type;
            if (type === 'string') {
                val = data.valStr
            } else if (type === 'list') {

            }else if (type === 'set') {

            }else if (type === 'hash') {
                data.val
            }
            console.log(val)
            $('textarea[name=val]').val(val);
        }

        /* 删除 */
        function doDel(obj) {
            layer.confirm('确定要删除选中数据吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                admin.req('/sys/sysRole/del', {
                    id: obj.data ? obj.data.id : '',
                    ids: obj.ids ? obj.ids.join(',') : ''
                }, function (res) {
                    layer.close(loadIndex);
                    if (0 === res.code) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.reload({page: {curr: 1}});
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                });
            });
        }

    });
</script>
</body>
#end